package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.RefundMapper;
import com.gxa.insurance.pojo.Refund;
import com.gxa.insurance.pojo.RefundType;
import com.gxa.insurance.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Min
 */
@Service
public class RefunserviceImpl implements RefundService {
    @Autowired
    private RefundMapper mapper;

    @Override
    public Map<String,Object> selectRefundByPage(Integer page, Integer pageSize) {
        // 处理默认数据
        if(page<=0 || page==null){
            page=1;
        }else if(pageSize<=0 || pageSize==null){
            pageSize=10;
        }
        // 处理起始页
        page=(page-1)*pageSize;
        // 处理
        List<Refund> refundList=mapper.selectAllByPage(page,pageSize);
        Integer count = mapper.selectCount();

        Map<String,Object> map=new HashMap<>();
        map.put("list",refundList);
        map.put("count",count);
        return map;
    }

    @Override
    public Map<String, Object> selectByStatus(String status, Integer page, Integer pageSize) {
        // 处理默认数据
        if(page<=0 || page==null){
            page=1;
        }else if(pageSize<=0 || pageSize==null){
            pageSize=10;
        }
        // 处理起始页
        page=(page-1)*pageSize;

        List<Refund> refundList=mapper.selectByStatus(status,page,pageSize);
        Integer count=mapper.selectCountByStatus();
        Map<String,Object> map=new HashMap<>();
        map.put("list",refundList);
        map.put("count",count);
        return map;
    }

    @Transactional
    @Override
    public Boolean updateIsReturn(Boolean returnMoney,String status,Integer id) {
        mapper.updateIsReturnAndStatus(returnMoney,status,id);
        return true;
    }

    @Transactional
    @Override
    public Boolean isRefuse(Map<String, String> map) throws BusinessException {
        if(!"true".equals(map.get("isRefuse"))  || !"false".equals(map.get("isRefuse"))){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isRefuse=Boolean.parseBoolean(map.get("isRefuse"));

        String reason=map.get("reason");
        Integer id = Integer.parseInt(map.get("id"));
        String status=map.get("status");
        //修改是否驳回
        mapper.updateIsRefuse(isRefuse,id);
        mapper.updateReasonAndStatus(reason,status,id);
        return true;
    }

    @Override
    public List<RefundType> selectAllStatus() {
       List<RefundType> types= mapper.selectAllTypes();
        return types;
    }


}
